<template>
  <div class="login-container">
    <el-card class="login-card">
      <h2>博客后台登录</h2>
      <el-form :model="form" @submit.prevent="login">
        <el-form-item label="用户名">
          <el-input v-model="form.username" />
        </el-form-item>
        <el-form-item label="密码">
          <el-input type="password" v-model="form.password" />
        </el-form-item>
        <el-button type="primary" @click="login">登录</el-button>
      </el-form>
    </el-card>
  </div>
</template>

<script setup>
import { reactive } from "vue";
import { useRouter } from "vue-router";
import axios from "axios";

const router = useRouter();
const form = reactive({ username: "", password: "" });

const login = async () => {
  const { data } = await axios.post("/api/login", form);
  if (data.success) {
    localStorage.setItem("token", data.token);
    router.push("/");
  } else {
    alert("登录失败");
  }
};
</script>

<style scoped>
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
}
.login-card {
  width: 300px;
}
</style>
